package com.qingyue.demo

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import androidx.collection.mutableIntListOf
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.qingyue.demo.ui.screen.LoginScreen
import com.qingyue.demo.ui.theme.DemoTheme

class MainActivity : ComponentActivity() {
    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // enableEdgeToEdge()
        // 界面入口
        setContent {
//            Column(
//                modifier = Modifier.fillMaxSize(),
//            ) {
//                Column(
//                    modifier = Modifier.weight(1f),
//                    verticalArrangement = Arrangement.Center,
//                    horizontalAlignment = Alignment.CenterHorizontally
//                ) {
//                    var count by remember { mutableIntStateOf(0) }
//                    Image(
//                        painter = painterResource(id = R.drawable.ic_image001),
//                        contentDescription = null,
//                        contentScale = ContentScale.Fit
//                    )
//                    StateLessSample(count = count) {
//                        count++
//                    }
//                }
//
//                Navbar(
//                    modifier = Modifier
//                        .background(Color(0x1A2196F3))
//                        .padding(16.dp)
//                )
//
//            }
            Column(
                modifier = Modifier
                    // 填充整个屏幕
                    .fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
//                verticalArrangement = Arrangement.Top
            ) {
                LoginScreen(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp, 48.dp, 16.dp, 0.dp)
                )
                LazyColumnSample(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(16.dp)
                )
            }


        }
    }
}

/**
 * 有状态组件
 */
@Composable
fun StateFullSample(modifier: Modifier = Modifier) {
    // Column 列布局: 垂直布局 -> 将里面的元素上下排列
    Column(
        // 填充整个屏幕
        modifier = Modifier.fillMaxSize(),
        // 水平剧中对齐
        horizontalAlignment = Alignment.CenterHorizontally,
        // 垂直剧中对齐
        verticalArrangement = Arrangement.Center
    ) {
        // 状态管理 state
        // 委托 by
        var count by remember { mutableIntStateOf(0) }
        Text(text = "$count", fontSize = 32.sp)
        Button(onClick = {
            count++
            println("点击了按钮 => count: $count")
        }, modifier = Modifier.padding(16.dp)) {
            Text(text = "按钮", fontSize = 16.sp)
        }
    }
}

/**
 * 无状态组件
 * 状态提升
 */
@Composable
fun StateLessSample(
    modifier: Modifier = Modifier,
    count: Int,
    onClick: () -> Unit
) {
    Column(
        // 填充整个屏幕
        modifier = Modifier.fillMaxWidth(),
        // 水平剧中对齐
        horizontalAlignment = Alignment.CenterHorizontally,
        // 垂直剧中对齐
        verticalArrangement = Arrangement.Center
    ) {
        // 状态管理 state
        // 委托 by
        // var count by remember { mutableIntStateOf(0) }
        Text(text = "$count", fontSize = 32.sp)
        Button(onClick = {
            onClick()
            println("点击了按钮 => count: $count")
        }, modifier = Modifier.padding(16.dp)) {
            Text(text = "按钮", fontSize = 16.sp)
        }
    }
}

@Composable
fun Navbar(
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceAround,
    ) {
        Button(onClick = {

        }) {
            Text(text = "首页")
        }
        Button(onClick = {

        }) {
            Text(text = "我的")
        }
        Button(onClick = {

        }) {
            Text(text = "设置")
        }
    }
}

@Composable
fun ScrollSample(modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp)
            // 上下滚动
            .verticalScroll(rememberScrollState()),
        // 垂直剧中对齐
        horizontalAlignment = Alignment.CenterHorizontally,
        // 水平居中对齐
        verticalArrangement = Arrangement.Center
    ) {
        repeat(50) {
            Text(text = "item ${it + 1}", fontSize = 32.sp)
        }
    }
}

@RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
@Composable
fun LazyColumnSample(modifier: Modifier = Modifier) {
    val itemList = remember {
        mutableStateListOf<Int>().apply {
            // addAll(1..1000)
        }
    }

//    LaunchedEffect(Unit) {
//        itemList.addAll(1..2000)
//    }

    LazyColumn(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        item {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceAround
            ) {
                Button(
                    modifier = Modifier,
                    colors = ButtonDefaults.buttonColors(Color(0xFF2196F3)),
                    onClick = {
                        itemList.add(itemList.size + 1)
                    }) {
                    Text(text = "添加")
                }
                Button(
                    modifier = Modifier,
                    colors = ButtonDefaults.buttonColors(Color(0xFFFF9800)),
                    onClick = {
                        if (itemList.isNotEmpty()) itemList.removeLast()
                    }) {
                    Text(text = "移除")
                }
                Button(
                    modifier = Modifier,
                    colors = ButtonDefaults.buttonColors(Color(0xFFE91E63)),
                    onClick = {
                        itemList.clear()
                    }) {
                    Text(text = "清空")
                }
            }
        }
        items(itemList) {
            Text(text = "item $it", fontSize = 32.sp)
        }
    }
}